<template>
	<view class="form" uni-show-loading="true" loading-type="circular">
		<uni-forms v-model="formData" ref="form">
			<uni-forms-item required label="姓名" name="username">
				<input type="text" v-model="formData.username" placeholder="请输入姓名" />
			</uni-forms-item>
			<uni-forms-item required label="年龄" name="age">
				<input type="number" v-model="formData.age" placeholder="请输入年龄" />
			</uni-forms-item>
			<uni-forms-item required name="phone" label="手机">
				<input type="number" v-model="formData.phone" placeholder="请输入手机号码" />
			</uni-forms-item>
			<uni-forms-item name="address" label="地址">
				<input type="text" v-model="formData.address" placeholder="请输入地址" />
			</uni-forms-item>
		</uni-forms>
		<button @click="submitForm" class="submit_btn">提交</button>
		<button @click="handleReset" class="reset_btn">重置</button>
	</view>
</template>

<script>
	import {
		validateChineseMobile
	} from '../../untils'
	import {
		formDefault
	} from './constant'
	export default {
		data() {
			return {
				formData: JSON.parse(JSON.stringify(formDefault)),
				rules: {
					// 对name字段进行必填验证
					username: {
						// name 字段的校验规则
						rules: [
							// 校验 name 不能为空
							{
								required: true,
								errorMessage: '请填写姓名',
							}
						]
					},
					password: {
						rules: [{
							required: true,
							errorMessage: '请输入密码'
						}]
					},
					age: {
						rules: [
							// 校验 name 不能为空
							{
								required: true,
								errorMessage: '请填写年龄',
							}
						]
					},
					phone: {
						rules: [
							// 校验 name 不能为空
							{
								required: true,
								errorMessage: '请填写手机号码',
								validateFunction: function(rule, value, data, callback) {
									if (!validateChineseMobile(value)) {
										callback('号码格式不正确，请重新填写')
									}
									return true
								}
							}
						]
					}
				}
			}
		},
		onReady() {
			// 需要在onReady中设置规则
			this.$refs.form.setRules(this.rules)
		},
		methods: {
			submitForm() {
				this.$refs.form.validate().then(async (form) => {
					try {
						uni.showLoading({
							title: '加载中',
							mask: true,
						})
						const options = {
							method: 'POST',
							headers: {
								"Content-Type": "application/json"
							},
							body: JSON.stringify(this.formData),
						}
						const res = await fetch('http://192.168.1.5:9080/api/userinfo/saveUserInfo', options)
							.then((response) => response.json())
							// .then((data) => {
							// 	// 在这里处理获取到的新闻数据，如将新闻标题显示在页面上
							// 	console.log(data);
							// })
						console.log('res', res)
						localStorage.setItem('userInfo', JSON.stringify(res.data))
						uni.showToast({
							title: '登陆成功'
						})
						uni.navigateTo({
							url:'/pages/success/index'
						})
					} finally {
						uni.hideLoading()
					}
				}).catch(err => {
					console.log('表单错误信息：', err);
				})
			},
			/** 重置表单 */
			handleReset() {
				this.formData = JSON.parse(JSON.stringify(formDefault))
				this.$refs.form.clearValidate()
			}
		}
	}
</script>

<style lang='scss' scoped>
	.form {
		uni-input {
			height: 100%;
		}

		.submit_btn {
			background-color: #FF684C;
			color: #fff;
		}

		.reset_btn {
			margin-top: 20rpx;
		}
	}
</style>